require.config({
    paths: {
        // angular
        "angular": "libs/angular/angular",
        // angular-ui
        "angular-ui-router": "libs/angular-ui-router/release/angular-ui-router",
        // angularAMD
        "angularAMD": "libs/angularAMD/angularAMD",
        "ngload": "libs/angularAMD/ngload",
        "jquery": "plugins/jQuery/jQuery-2.1.4.min",
        "bootstrap-min": "bootstrap/js/bootstrap.min",
        "adminlte": "dist/js/adminlte",
        "slimscroll": "plugins/slimScroll/jquery.slimscroll.min",
        "fastclick": "plugins/fastclick/fastclick.min",
        "ui-bootstrap-tpls": "bootstrap/js/ui-bootstrap-tpls-0.13.4"
    },
    shim: {
        "angular": {exports: "angular"},
        "angular-ui-router": ["angular"],
        "angularAMD": ["angular"],
        "ngload": ["angularAMD"],
        "jquery": {exports: "jquery"},
        "bootstrap-min": ["jquery"],
        "adminlte": ["bootstrap-min"],
        "slimscroll": ["jquery"],
        "fastclick": ["jquery"],
        "ui-bootstrap-tpls": ["angular"]
    },
    urlArgs: "bust=" + (new Date()).getTime() //防止读取缓存，调试用

});
// bootstrap
define(["angular", "angularAMD", "angular-ui-router", "jquery", "bootstrap-min", "slimscroll", "fastclick", "ui-bootstrap-tpls"], function (angular, angularAMD) {
    // module
    var app = angular.module("app", ["ui.router", "ui.bootstrap"]);

    app.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.transformRequest = function(request){
            if(typeof(request)!='object'){
                return request;
            }
            var str = [];
            for(var k in request){
                if(k.charAt(0)=='$'){
                    delete request[k];
                    continue;
                }
                var v='object'==typeof(request[k])?JSON.stringify(request[k]):request[k];
                str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
            }
            return str.join("&");
        };
        $httpProvider.defaults.timeout=10000;
        $httpProvider.defaults.headers.post = {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
            'X-Requested-With': 'XMLHttpRequest'
        };


        $httpProvider.interceptors.push(function ($q, $location, $window, $rootScope) {
            return {
                response: function (response) {
                    if (response.data.code == 'LOGIN_TIMEOUT') {
                        var url = $location.absUrl();
                        url = url.substr(0, url.lastIndexOf("/", url.lastIndexOf("#"))) + "/login.html";
                        $window.location.href = url;
                    } else if (response.data.msg != null && response.data.status == false) {
                        var scope = $rootScope.$new();

                        scope.alert = {
                            header: "提示",
                            msg: response.data.msg
                        }
                        $rootScope.modalInstance.open({
                            templateUrl: 'AlertModal.html',
                            controller: "AlertCtrl",
                            scope: scope
                        });
                    }
                    return response;
                }
            }
        });
    }]);

    app.controller('AlertCtrl', function ($scope, $modalInstance) {
        $scope.ok = function () {
            $modalInstance.close();
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    });
    app.controller('layoutCtrl', function ($rootScope, $http, $scope, $modal) {
        $rootScope.modalInstance = $modal;
        $http.post('menu/list.do', {}).success(function (data) {
            $rootScope.baseinfo = data.data;
        });
    });
    app.filter('cut', function () {
        return function (value, wordwise, max, tail) {
            if (!value) return '';

            max = parseInt(max, 10);
            if (!max) return value;
            if (value.length <= max) return value;

            value = value.substr(0, max);
            if (wordwise) {
                var lastspace = value.lastIndexOf(' ');
                if (lastspace != -1) {
                    value = value.substr(0, lastspace);
                }
            }

            return value + (tail || ' …');
        };
    });
    // routes
    app.config(["$stateProvider", "$urlRouterProvider", '$locationProvider', function ($stateProvider, $urlRouterProvider) {
        // default
        $urlRouterProvider.otherwise("/application");
        // route
        $stateProvider
            // home
            .state("home", angularAMD.route({
                url: "/home",
                templateUrl: "views/home.html"
            }))
            .state("application", angularAMD.route({
                url: "/application",
                templateUrl: "views/application/list.html",
                controller: 'AppCtrl',
                controllerUrl: "controllers/application.js"
            }))
            .state("userlist", angularAMD.route({
                url: "/userlist",
                templateUrl: "views/user/list.html",
                controller: 'UserCtrl',
                controllerUrl: "controllers/user.js"
            }))
            .state("tasklist", angularAMD.route({
                abstract: true,
                url:"/tasklist",
                template: "<ui-view/>"
            }))
            .state("tasklist.detail", angularAMD.route({
                url: "/{appId:[0-9]{1,8}}",
                templateUrl: "views/schedule/taskList.html",
                controller: 'TaskCtrl',
                controllerUrl: "controllers/task.js",
                parent: "tasklist"
            }))
        ;
    }]);
    // bootstrap
    angularAMD.bootstrap(app)
    return app;
});